package com.cuotibao.zhuoxiao.basics;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.multidex.MultiDex;
import android.util.Log;

import com.bumptech.glide.Glide;
import com.cuotibao.zhuoxiao.basics.utils.statck.ActivityManageImpl;
import com.tencent.bugly.crashreport.CrashReport;
import com.zxy.tiny.Tiny;

/**
 * @author hdx on 2019/4/25.
 */
public abstract class BaseApplication extends Application implements Application.ActivityLifecycleCallbacks {
    private static BaseApplication sInstance;
    private static Context mContext;
    public static ActivityManageImpl mActivityManage;
    public static int startCount = 0;
    protected abstract void activityDataSave(Activity activity, Bundle outState);
    protected abstract void activityDataReset(Activity activity, Bundle savedInstanceState);

    public static BaseApplication getIns() {
        return sInstance;
    }

    public abstract String getOrganizationUrl();

    public abstract boolean getLoginState();

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(newBase);
        MultiDex.install(this);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        if (isMainProcess()) {
            sInstance = this;
            mContext = this.getApplicationContext();
            mActivityManage = new ActivityManageImpl();
            registerActivityLifecycleCallbacks(this);
            CrashReport.initCrashReport(getApplicationContext(), "75552d89e0", false);
            Tiny.getInstance().init(this);
//            if (LeakCanary.isInAnalyzerProcess(this)) {
//                // This process is dedicated to LeakCanary for heap analysis.
//                // You should not init your app in this process.
//                return;
//            }
//            LeakCanary.install(this);
        }
    }

    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
        Glide.get(this).onTrimMemory(level);
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        Glide.get(this).onLowMemory();
    }

    public static Context getMyContext() {
        return mContext;
    }

    public static boolean isBackground() {
        if (startCount == 0) {
            return false;
        } else {
            return true;
        }
    }


    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        if (mActivityManage == null) {
            mActivityManage = new ActivityManageImpl();
        }
        mActivityManage.pushActivity(activity);
        activityDataReset(activity,savedInstanceState);
    }

    @Override
    public void onActivityStarted(Activity activity) {
        startCount++;
    }

    @Override
    public void onActivityResumed(Activity activity) {

    }

    @Override
    public void onActivityPaused(Activity activity) {

    }

    @Override
    public void onActivityStopped(Activity activity) {
        startCount--;
        if (!isBackground()) {
            Glide.get(this).clearMemory();
        }
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
        activityDataSave(activity,outState);
    }

    @Override
    public void onActivityDestroyed(Activity activity) {
        mActivityManage.popActivity(activity);
    }


    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        if (newConfig.fontScale != 1)//非默认值
            getResources();
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public Resources getResources() {
        Resources resources = super.getResources();
        if (resources != null && resources.getConfiguration().fontScale != 1.0f) {
            android.content.res.Configuration configuration = resources.getConfiguration();
            configuration.fontScale = 1.0f;
            resources.updateConfiguration(configuration, resources.getDisplayMetrics());
        }
        return resources;
    }


    /**
     * 包名判断是否为主进程
     *
     * @param
     * @return
     */
    public boolean isMainProcess() {
        return getApplicationContext().getPackageName().equals(getCurrentProcessName());
    }

    /**
     * 获取当前进程名
     */
    private String getCurrentProcessName() {
        int pid = android.os.Process.myPid();
        String processName = "";
        ActivityManager manager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo process : manager.getRunningAppProcesses()) {
            if (process.pid == pid) {
                processName = process.processName;
            }
        }
        return processName;
    }
}
